<?php

namespace App\Http\Controllers\Admin;

use App\Cate;
use App\Chapter;
use App\SubFour;
use Illuminate\Http\Request;


class SubFourController extends Base
{
    public function GetSubFourCate(Request $request)
    {
        if ($request->isMethod('post')){
            //查找科目四的分类
            $list = Cate::where('cate',4)->get();
            return $this->response->array($list);
        }
    }



    public function GetSubFourChapters(Request $request)
    {
        if ($request->isMethod('post')){
            //查找科目四的分类
            $list = Chapter::where('cate',4)->get();
            return $this->response->array($list);
        }
    }


    public function SubFourAdd(Request $request)
    {
        $map = $request->input();
        $map['answer'] = implode(',',$map['answer']);
        $map['cate_id'] = implode(',',$map['cate_id']);
        $model = SubFour::add($map);
        return $model;
    }


    public function GetSubFourList(Request $request)
    {

        $page  = $request->input('page');


        $list  = SubFour::with('image')
            ->with('chapters')
            ->offset(($page-1)*30)
            ->limit(30)
            ->get()
            ->toArray();
        foreach ($list as $k=>$v){
            if($v['url']==''){
                $list[$k]['image']['url']='';
            }
        }
        $cate_id = '';

        foreach ($list as $k=>$v){
            foreach (explode(',',$v['cate_id']) as $ki=>$vi){
                $list[$k]['cate'][] = Cate::where('id',$vi)->first();
            }

        }

        foreach ($list as $k=>$v){


            $list[$k]['is_zy']==1?$list[$k]['is_zy']=true:$list[$k]['is_zy']=false;
            $list[$k]['is_yct']==1?$list[$k]['is_yct']=true:$list[$k]['is_yct']=false;

        }
        $zpage = SubFour::count();

        return $this->response->array([
            'data'=>$list,
            'total'=>$zpage,
        ]);
    }




    /**
     * 修改争议题状态
     * @param Request $request
     * @return mixed
     */
    public function SubFourZy(Request $request)
    {
        if($request->isMethod('post')){
            $map = $request->input();
            $rwe = SubFour::where('id',$map['id'])->update($map);
            return $rwe;
        }
    }


    /**
     * 修改易错题状态
     * @param Request $request
     * @return mixed
     */
    public function SubFourYc(Request $request)
    {
        if($request->isMethod('post')){
            $map = $request->input();
            $rwe = SubFour::where('id',$map['id'])->update($map);
            return $rwe;
        }
    }


    /**
     * @param Request $request
     * @return mixed
     * 科目一列表
     */
    public function SubFourSearch(Request $request)
    {
        //获取传过来的页码
//        $page  = $request->input('page');
        if($request->input('chapters_id')==''){
            $list  = SubFour::with('image')
                ->with('chapters')
                ->where('question','like','%'.$request->input('title').'%')
                ->limit(10)
                ->get()

                ->toArray();
        }else{
            $list  = SubFour::with('image')
                ->with('chapters')
                ->where('question','like','%'.$request->input('title').'%')
                ->where('chapters_id',$request->input('chapters_id'))
                ->limit(10)
                ->get()

                ->toArray();
        }


        //如果没有图片返回空
        foreach ($list as $k=>$v){
            if($v['url']==''){
                $list[$k]['image']['url']='';
            }
        }
        $cate_id = '';
        //获取多个分类
        foreach ($list as $k=>$v){
            foreach (explode(',',$v['cate_id']) as $ki=>$vi){
                $list[$k]['cate'][] = Cate::where('id',$vi)->first();
            }
        }

        foreach ($list as $k=>$v){
            //判断是否为争议和易错题
            $list[$k]['is_zy']==1?$list[$k]['is_zy']=true:$list[$k]['is_zy']=false;
            $list[$k]['is_yct']==1?$list[$k]['is_yct']=true:$list[$k]['is_yct']=false;

        }
        if($request->input('chapters_id')==''){
            //总数据
            $zpage = SubFour::
            where('question','like','%'.$request->input('title').'%')
                ->limit(10)
                ->count();
        }else{
            //总数据
            $zpage = SubFour::
            where('question','like','%'.$request->input('title').'%')
                ->where('chapters_id',$request->input('chapters_id'))
                ->limit(10)
                ->count();
        }

        //返回当前页码数据和总数
        return $this->response->array([
            'data'=>$list,
            'total'=>10,
            'session'=>session('adminuser'),
        ]);
    }



    public function GetSubFourInfo(Request $request)
    {

        $id = $request->input('id');
        $list = SubFour::with('image')->where('id',$id)->first();
        $list['answer'] = explode(',',$list['answer']);
        $list['cate_id'] = explode(',',$list['cate_id']);
        foreach ($list['cate_id'] as $v){
            $cate_id[] = intval($v);
        }
        $list['cate_id'] = $cate_id;
        if($list['url']!=''){
            $image['name'] = $list['question'];
            $image['url'] = $list['image']['url'];
            $images[] = $image;
        }else{
            $images = '';
        }

        return $this->response->array([
            'data'=> $list,
            'image'=>$images
        ]);
    }


    public function SubFourSave(Request $request)
    {
        if($request->isMethod('post')){

            $map = $request->input();
            unset($map['image']);
            $map['answer'] = implode(',',$map['answer']);
            $map['cate_id'] = implode(',',$map['cate_id']);
            $rwe = SubFour::where('id',$map['id'])->update($map);
            return $rwe;
        }
    }


    public function SubFourdels(Request $request)
    {
        if($request->isMethod('post')){
            $id = $request->input('id');
            SubFour::where('id',$id)->delete();
        }
    }
}
